﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.Common;




namespace Actuals
{
    public partial class AddUserStory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            if (!IsPostBack)
            {
               
              

                if (Request.QueryString["Mode"] != null)
                {
                      string _mode = (Request.QueryString["Mode"].ToString());
                    ViewState["Mode"] = _mode;
                    if (_mode == "Add" || _mode == "Edit")
                    {
                        string _taskId = Request.QueryString["TaskId"].ToString();

                        IBusinessTask businessTask = new BusinessTask();
                        ITask task = businessTask.GetTask(int.Parse(_taskId));

                        int _projectId = task.ProjectId;
                        int _releaseId = task.ReleaseId;
                        ViewState["ProjectId"] = _projectId;
                        ViewState["ReleaseId"] = _releaseId;
                      

                        if (_mode == "Add")
                        {
                            //string _taskId = Request.QueryString["TaskId"].ToString();
                            ViewState["TaskId"] = _taskId;


                        }
                        else if (_mode == "Edit")
                        {
                            //string _taskId = Request.QueryString["TaskId"].ToString();
                            ViewState["TaskId"] = _taskId;
                            string _userStoryId = Request.QueryString["UserStoryId"].ToString();
                            hidUserStoryId.Value = _userStoryId;
                            ddlTask.Enabled = true;
                            PopulateUserStory(int.Parse(_userStoryId));
                        }
                    }
                  
                    
                    else if (_mode == "AddForSprint")
                    {
                        string _projectId = Request.QueryString["ProjectId"].ToString();
                        string _releaseId = Request.QueryString["ReleaseId"].ToString();
                        ViewState["ProjectId"] = _projectId;
                        ViewState["ReleaseId"] = _releaseId;

                        string _sprintId = Request.QueryString["SprintId"].ToString();
                        //string _projectId = Request.QueryString["ProjectId"].ToString();
                        //string _releaseId = Request.QueryString["ReleaseId"].ToString();
                        ViewState["SprintId"] = _sprintId;

                        ddlTask.Enabled = true;
                        PopulateForSprint(int.Parse(_sprintId), int.Parse(_projectId), int.Parse(_releaseId));
                    }

                    IBusinessSprint businessSprint = new BusinessSprint();
                    IList<ISprint> sprints = businessSprint.GetSprints(int.Parse(ViewState["ProjectId"].ToString()),
                                                               int.Parse(ViewState["ReleaseId"].ToString()));
                    ddlSprint.DataSource = sprints;
                    ddlSprint.DataTextField = "SprintDescription";
                    ddlSprint.DataValueField = "SprintId";
                    ddlSprint.DataBind();
                    ddlSprint.Items.Insert(0, "Select");

                }

                    
            }
        }

       protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
               
                IUserStory userStory = new UserStory();
                
                userStory.UserStoryDescription = txtUserStoryDescription.Text;
                userStory.Sprint = int.Parse(ddlSprint.SelectedValue).ToString();
                userStory.StoryPoints = int.Parse(ddlStoryPoints.SelectedValue.ToString());
                IBusinessUserStory businessUserStory = new BusinessUserStory();
                if(ViewState["Mode"].ToString()=="Edit")
                {
                    userStory.TaskId = int.Parse(ddlTask.SelectedValue);
                    userStory.UserStoryId =int.Parse(hidUserStoryId.Value);

                    businessUserStory.UpdateUserStory(userStory);
                    lblMsg.Text = "Updated";
                    }
                else if (ViewState["Mode"].ToString() == "Add")
                {

                    userStory.TaskId = (int.Parse(ViewState["TaskId"].ToString()));
                    businessUserStory.AddUserStory(userStory);
                    lblMsg.Text = "Saved";
                }
                else if (ViewState["Mode"].ToString() == "AddForSprint")
                {

                    userStory.TaskId = int.Parse(ddlTask.SelectedValue);
                    businessUserStory.AddUserStory(userStory);
                    lblMsg.Text = "Saved";
                }

            }
           catch(Exception ex)
            {
               lblMsg.Text = ex.Message;
           }

        }

       protected void PopulateForSprint(int sprintId, int projectId, int releaseId)
       {
           IBusinessTask businessTask = new BusinessTask();
           IList<ITask> _tasks=  businessTask.GetTasks(projectId, releaseId);
           ddlTask.DataSource = _tasks;
           ddlTask.DataValueField = "TaskId";
           ddlTask.DataTextField = "TaskShortName";

           ddlTask.DataBind();

           ddlSprint.SelectedValue = ViewState["SprintId"].ToString();
       }
        protected void PopulateUserStory(int userStoryId)
        {
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IUserStory userStory = businessUserStory.GetUserStory(userStoryId);
            txtUserStoryDescription.Text = userStory.UserStoryDescription;
            ddlSprint.SelectedValue = userStory.Sprint.ToString();
            ddlStoryPoints.SelectedValue = userStory.StoryPoints.ToString();
            IBusinessTask businessTask = new BusinessTask();
            IList<ITask> tasks = businessTask.GetTasks(int.Parse(ViewState["ProjectId"].ToString()),
                                                int.Parse(ViewState["ReleaseId"].ToString()));
            ddlTask.DataSource = tasks;
            ddlTask.DataTextField = "TaskShortName";
            ddlTask.DataValueField = "TaskId";
            ddlTask.DataBind();
            ddlTask.SelectedValue =ViewState["TaskId"].ToString();


        }


    }

}